<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MX_Controller {

	public function __construct()
	{
		parent::__construct();
                $this->load->helper(array("url","form","my_data"));
                $this->load->library(array("form_validation","session","my_auth","email","grocery_CRUD"));

                if(!$this->my_auth->is_Admin()){
                    redirect(site_url()."/admin/verify/login");
                    exit();
                }
                $this->load->library("my_layout"); // Sử dụng thư viện layout
                $this->my_layout->setLayout("layout/frontend_admin"); // load file layout chính (views/frontend.php)
	}
        public function _example_output($output = null)
	{
                
		 $this->my_layout->view('admin_view/home',$output);
	}

        public function index()
        {
            try{
                $crud = new grocery_CRUD();
                //$crud->set_theme('datatables');
                $crud->set_table('users');
                $crud->columns('id_user','first_name','last_name','username',
                        'level','email','phone','address','url_image');
                $crud->required_fields('password','username','level','id_sex');
                $crud->unset_edit_fields('id_user','password');
                $crud->set_field_upload('url_image','public/images/users');
                $crud->callback_column('url_image',array($this,'_callback_url'));
                $crud->callback_before_update(array($this,'addpath'));
                //$crud->callback_after_update(array($this,'delpath'));
                $crud->callback_before_insert(array($this,'addpath'));
                $output = $crud->render();
                
            
                $this->_example_output($output);
            } catch (Exception $e) {
                show_error($e->getMessage().' --- '.$e->getTraceAsString());
            }
            
        }
        public function centers()
        {
            try{
                $crud = new grocery_CRUD();

                //$crud->set_theme('datatables');
                $crud->set_table('center');
                $crud->set_subject('centers');
                 $crud->required_fields('name','discription','contact','phone','create_date',
                 'url_image','id_location', 'id_type_center','id_manager');
                 
                $crud->set_field_upload('url_image','public/images/tinhnguyen');
                $crud->callback_column('url_image',array($this,'_callback_url'));
                $crud->callback_before_update(array($this,'addpath'));
                //$crud->callback_after_update(array($this,'delpath'));
                $crud->callback_before_insert(array($this,'addpath'));
                $crud->columns('id_center','name','discription','contact','phone','create_date','url_image');
                
                $output = $crud->render();

                $this->_example_output($output);

                }catch(Exception $e){
                    show_error($e->getMessage().' --- '.$e->getTraceAsString());
                }
        }
        
      
        // articles
        public function articles()
        {
            $crud = new grocery_CRUD();
            $crud->set_table('article');
            
            $crud->required_fields('title','date','source','id_author');
            $crud->unset_edit_fields('id_user','password');
            $crud->set_field_upload('url_image','public/images/tinhnguyen');
            $crud->callback_column('url_image',array($this,'_callback_url'));
            $crud->callback_before_update(array($this,'addpath'));
            //$crud->callback_after_update(array($this,'delpath'));
            $crud->callback_before_insert(array($this,'addpath'));
            $output = $crud->render();
            $this->_example_output($output);
        }//end articles
        
        // activities
        public function activities()
        {
            try{
                $crud = new grocery_CRUD();
                $crud->set_table('activity');
                $crud->required_fields('name','discription','startday','endday','id_center');
                $crud->unset_edit_fields('id_user','password');
                $crud->set_field_upload('url_image','public/images/activities');
                $crud->callback_column('url_image',array($this,'_callback_url'));
                $crud->callback_before_update(array($this,'addpath'));
                //$crud->callback_after_update(array($this,'delpath'));
                $crud->callback_before_insert(array($this,'addpath'));
                $output = $crud->render();
                $this->_example_output($output);
            } catch (Exception $e) {
                show_error($e->getMessage().' --- '.$e->getTraceAsString());
            }
            
        }//end activities
        
        function _callback_url($value)
        {
            $value = str_replace("public/images/tinhnguyen/", "", $value);
            return "<img src='". base_url('public/images/tinhnguyen/'.$value)."'  height='50px' /> ";
        }
        
        function addpath($post_array) 
        {
            if (!empty($post_array['url_image'])) {
                $post_array['url_image'] = 'public/images/tinhnguyen/'.$post_array['url_image'];
            }
            return $post_array;
        }
}